// pages/map/map.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    latitude: 23.099994,
    longitude: 113.324520,
    markers: [{
      id: 1,
      latitude: 23.099994,
      longitude: 113.324520,
      name: 'T.I.T 创意园'
    }],
    covers: [{
      latitude: 23.099994,
      longitude: 113.344520,
    }, {
      latitude: 23.099994,
      longitude: 113.304520,
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    this.mapCtx = wx.createMapContext('myMap')
  },
  // 获取当前位置
  getCenterLocation: function () {
    let that = this
    console.log(app.data.address.name)
    that.setData({
      city: app.data.address.name || "西安"
    })
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var accuracy = res.accuracy
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        console.log(latitude, longitude, accuracy)
      }
    })
  },
  // 获取位置路线
  getPolyline: function () {
    const that = this;
    wx.request({
      url: `https://apis.map.qq.com/ws/direction/v1/bicycling/?from=${wx.getStorageSync("startAddress").latitude},${wx.getStorageSync("startAddress").longitude}&to=34.28,108.94&key=Z6RBZ-6H4CU-AV6V5-2OTIO-Q2CLZ-VMF5T`,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var coors = res.data.result.routes[0].polyline;
        for (var i = 2; i < coors.length; i++)
        { coors[i] = coors[i - 2] + coors[i] / 1000000 }
        console.log(coors);
        const points = [];
        for (var j = 0; j < coors.length; j += 2) {
          points[points.length] = { longitude: coors[j + 1], latitude: coors[j] }
        }
        const polyline = that.data.polyline;
        polyline[0].points = points;
        that.setData({ polyline });
      },
      fail: function () {
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})